package kuzha.code.jvm;

import kuzha.KuzhaObject;
import kuzha.KuzhaSystemException;
import kuzha.code.CodeBlock;
import kuzha.except.VoidOperationException;
import kuzha.runtime.KzInnerContext;

public abstract class JvmCodeBlock extends CodeBlock {
	protected JvmCodeBlock() {
		super();
	}
	
	protected void raiseVoidException(RuntimeException ex) {
		StackTraceElement[] sts = ex.getStackTrace();
		if (sts.length == 0 || sts[0].getClassName().equals(getClass().getName())) {
			KuzhaSystemException ne = new VoidOperationException();
			ne.setStackTrace(sts);
			throw ne;
		}
		throw ex;
	}
	
	protected static KuzhaObject pushContext(KuzhaObject context) {
		KzInnerContext inner = new KzInnerContext(context);
		return inner;
	}
	protected static KuzhaObject pushContext(KuzhaObject context, String var) {
		KzInnerContext inner = new KzInnerContext(context);
		inner.addLet(var);
		return inner;
	}
	protected static KuzhaObject popContext(KuzhaObject context) {
		if (context instanceof KzInnerContext) {
			KzInnerContext inner = (KzInnerContext) context;
			return inner.outer;
		}
		return context;
	}
}
